package com.sicnu.cheer.leaugebar.leaguemodle.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sicnu.cheer.generalmodule.activity.BaseActivity;
import com.sicnu.cheer.generalmodule.adapter.base.CommonAdapter;
import com.sicnu.cheer.generalmodule.adapter.viewholder.CommonViewHolder;
import com.sicnu.cheer.generalmodule.bean.AlbumBean;
import com.sicnu.cheer.generalmodule.bean.MyUser;
import com.sicnu.cheer.generalmodule.config.Constant;
import com.sicnu.cheer.generalmodule.util.NetWorkUtil;
import com.sicnu.cheer.generalmodule.util.UIHelper;
import com.sicnu.cheer.leaugebar.leaguemodle.R;
import com.sicnu.cheer.xlistview.view.XListView;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

public class AlbumActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout noResultLL;
    private XListView xListView;
    private CommonAdapter mAdapter;
    private List<AlbumBean> list;
    private ProgressDialog progressDialog;
    private int pageSize = 10;
    private int skip = 0;

    @Override
    public int getContentViewId() {
        return R.layout.activity_my_album;
    }

    @Override
    protected void initView() {
        super.initView();
        ((TextView) findViewById(R.id.title)).setText("社团相册");
        findViewById(R.id.back).setOnClickListener(this);
        noResultLL = ((LinearLayout) findViewById(R.id.no_data_ll));
        noResultLL.setVisibility(View.GONE);
        findViewById(R.id.create_album_btn).setOnClickListener(this);
        xListView = ((XListView) findViewById(R.id.data_xlv));
        xListView.setPullLoadEnable(true);
        xListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                pageSize = 10;
                skip = 0;
                requestDataFromBmob(true);
            }

            @Override
            public void onLoadMore() {
                pageSize += 10;
                skip += 10;
                requestDataFromBmob(false);
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        list = new ArrayList<>();
        progressDialog = new ProgressDialog(mThis);
        progressDialog.setMessage("加载中...");
    }

    private void requestDataFromBmob(final boolean isRefresh) {
        resetState();
        if (!NetWorkUtil.isConnect(mThis)) {
            UIHelper.ToastMessage(mThis, "没有网络，请设置网络");
            return;
        }
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
        BmobQuery<AlbumBean> query = new BmobQuery<AlbumBean>();
        query.addWhereEqualTo("owner", MyUser.getCurrentUser(mThis, MyUser.class).getLeague());
        query.setLimit(pageSize);
        query.setSkip(skip);
        query.order("-createdAt");
        query.findObjects(mThis, new FindListener<AlbumBean>() {
            @Override
            public void onSuccess(List<AlbumBean> object) {
                stopXListView(isRefresh);
                if (object==null||object.size()==0){
                    setNoResultState(isRefresh);
                }else {
                    if (isRefresh){
                        list.clear();
                    }
                    list.addAll(object);
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(int code, String msg) {
                stopXListView(isRefresh);
                UIHelper.ToastMessage(mThis, "查询失败：" + msg);
            }
        });
    }

    private void resetState() {
        noResultLL.setVisibility(View.GONE);
        xListView.setVisibility(View.VISIBLE);
        xListView.setPullLoadEnable(true);
    }

    private void setNoResultState(boolean isRefresh) {
        if (isRefresh){
            noResultLL.setVisibility(View.VISIBLE);
            xListView.setVisibility(View.GONE);
        }else {
            xListView.setPullLoadEnable(false);
            UIHelper.ToastMessage(mThis, "没有更多数据了");
        }
    }

    private void stopXListView(boolean isRefresh) {
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        if (isRefresh){
            xListView.stopRefresh();
        }else {
            xListView.stopLoadMore();
        }

    }

    @Override
    protected void afterViews() {
        super.afterViews();
        requestDataFromBmob(true);
        //将获取到的数据填充界面
        xListView.setAdapter(mAdapter = new CommonAdapter<AlbumBean>(
                mThis, list, R.layout.item_album) {
            @Override
            public void convert(CommonViewHolder helper, AlbumBean item) {
                if (item != null) {
                    helper.setText(R.id.album_name_tv, item.getName());
                    String latestImgUrl = "";
                    int size = 0;
                    if (item.getImgUrls() != null&&item.getImgUrls().size()!=0) {
                        latestImgUrl = item.getImgUrls().get(0).toString();
                        size = item.getImgUrls().size();
                    }
                    helper.setImageByUrl(R.id.latest_img, latestImgUrl, R.mipmap.default_pic);
                    helper.setText(R.id.file_count_tv, size + "张");
                }
            }

        });

        xListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mThis, PhotoActivity.class);
                intent.putExtra("bean", list.get(position - 1));
                startActivityForResult(intent, Constant.ALBUM_RESULT_FROM);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            onBackPressed();
        } else if (v.getId() == R.id.create_album_btn) {
            Intent intent = new Intent(mThis, CreateAlbumActivity.class);
            startActivityForResult(intent, Constant.ALBUM_RESULT_FROM);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ALBUM_RESULT_FROM&&resultCode==Constant.ALBUM_RESULT_FROM) {
            skip=0;
            requestDataFromBmob(true);
            setResult(Constant.ALBUM_RESULT_FROM);
        }
    }
}
